package cn.ecasoft.common.message.utils;

import cn.ecasoft.common.message.ApiHelper;
import cn.ecasoft.common.message.GtApiConfiguration;
import cn.ecasoft.common.message.api.PushApi;
import cn.ecasoft.common.message.common.ApiResult;
import cn.ecasoft.common.message.config.GeTuiConfiguration;
import cn.ecasoft.common.message.dto.CommonEnum;
import cn.ecasoft.common.message.dto.req.Audience;
import cn.ecasoft.common.message.dto.req.Settings;
import cn.ecasoft.common.message.dto.req.Strategy;
import cn.ecasoft.common.message.dto.req.message.PushChannel;
import cn.ecasoft.common.message.dto.req.message.PushDTO;
import cn.ecasoft.common.message.dto.req.message.PushMessage;
import cn.ecasoft.common.message.dto.req.message.android.AndroidDTO;
import cn.ecasoft.common.message.dto.req.message.android.GTNotification;
import cn.ecasoft.common.message.dto.req.message.android.ThirdNotification;
import cn.ecasoft.common.message.dto.req.message.android.Ups;
import cn.ecasoft.common.message.dto.req.message.ios.Alert;
import cn.ecasoft.common.message.dto.req.message.ios.Aps;
import cn.ecasoft.common.message.dto.req.message.ios.IosDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author: CaiZhiGang
 * @Title: PushMessageUtil
 * @ProjectName: servers
 * @Description: 推送消息工具类
 * @date: 2021/7/16 10:04
 */
@Component
public class PushMessageUtil {

    @Autowired
    private GeTuiConfiguration geTuiConfiguration;

    /**
     * 根据cid推送
     * e.g
     * {
     * "request_id":"xxx",
     * "settings":{
     * "ttl":3600000
     * },
     * "audience":{
     * "cid":[
     * "xxx"
     * ]
     * },
     * "push_message":{
     * "notification":{
     * "title":"请填写通知标题",
     * "body":"请填写通知内容",
     * "click_type":"url",
     * "url":"https//:xxx"
     * }
     * }
     * }
     *
     * @param pushDTO
     * @return ApiResult<Map < String, Map < String, String>>>
     */
    public ApiResult<Map<String, Map<String, String>>> push(PushDTO<Audience> pushDTO) {
        PushApi pushApi = getApiHelper().creatApi(PushApi.class);
        ApiResult<Map<String, Map<String, String>>> mapApiResult = pushApi.pushToSingleByCid(pushDTO);
        return mapApiResult;
    }

    private ApiHelper getApiHelper() {
        GtApiConfiguration gtApiConfiguration = getGtApiConfiguration();
        return ApiHelper.build(gtApiConfiguration);
    }

    private GtApiConfiguration getGtApiConfiguration() {
        GtApiConfiguration gtApiConfiguration = new GtApiConfiguration();
        gtApiConfiguration.setAppId(geTuiConfiguration.getAppId());
        gtApiConfiguration.setAppKey(geTuiConfiguration.getAppkey());
        gtApiConfiguration.setMasterSecret(geTuiConfiguration.getMasterSecret());
        return gtApiConfiguration;
    }


    public ApiResult<Map<String, Map<String, String>>> sendGeTuiMessage(String cid, String title, String body) {
        PushDTO<Audience> audiencePushDTO = new PushDTO<>();
        audiencePushDTO.setRequestId(String.valueOf(System.currentTimeMillis()));
        audiencePushDTO.setSettings(getSettings());
        audiencePushDTO.setAudience(getAudience(cid));
        audiencePushDTO.setPushMessage(getPushMessage(title, body));
        audiencePushDTO.setPushChannel(getPushChannel(title, body));
        return this.push(audiencePushDTO);
    }

    private PushChannel getPushChannel(String title, String body) {
        PushChannel pushChannel = new PushChannel();
        pushChannel.setIos(getIosDTO(title, body));
        pushChannel.setAndroid(getAndroidDTO(title, body));
        return pushChannel;
    }

    private AndroidDTO getAndroidDTO(String title, String body) {
        AndroidDTO androidDTO = new AndroidDTO();
        androidDTO.setUps(getUps(title, body));
        return androidDTO;
    }

    private Ups getUps(String title, String body) {
        Ups ups = new Ups();
        ups.setNotification(new ThirdNotification() {
            {
                setTitle(title);
                setBody(body);
                setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
            }
        });
        return ups;
    }

    private IosDTO getIosDTO(String title, String body) {
        IosDTO iosDTO = new IosDTO();
        iosDTO.setType("notify");
        iosDTO.setPayload("浙里建");
        Aps aps = new Aps();
        aps.setAlert(new Alert() {
            {
                setTitle(title);
                setBody(body);
            }
        });
        aps.setContentAvailable(0);
        iosDTO.setAps(aps);
        iosDTO.setAutoBadge("+1");
        return iosDTO;
    }

    private Audience getAudience(String cid) {
        Audience audience = new Audience() {
            {
                setCid(new ArrayList<String>() {
                    {
                        add(cid);
                    }
                });
            }
        };
        return audience;
    }

    private PushMessage getPushMessage(String title, String body) {
        PushMessage pushMessage = new PushMessage();
        pushMessage.setNotification(getGtNotification(title, body));
        return pushMessage;
    }

    private GTNotification getGtNotification(String title, String body) {
        GTNotification gtNotification = new GTNotification();
        gtNotification.setTitle(title);
        gtNotification.setBody(body);
        gtNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
        return gtNotification;
    }

    private Settings getSettings() {
        Settings settings = new Settings();
        settings.setTtl(3600000);
        Strategy strategy = new Strategy();
        strategy.setDef(1);
        strategy.setIos(4);
        strategy.setSt(4);

        settings.setStrategy(strategy);
        return settings;
    }
}
